package tr.edu.metu.is550.isell.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import tr.edu.metu.is550.isell.R;

public class RegisterUserActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_user);

        Button viewById = (Button) findViewById(R.id.btnRegisterSubmit);
        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);


        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String serverURL = sp.getString("pref_dialog", "NULL");

                    DefaultHttpClient client = new DefaultHttpClient();

                    JSONObject user = new JSONObject();
                    JSONObject detail = new JSONObject();
                    detail.put("name", ((EditText) findViewById(R.id.txtName)).getText())
                            .put("surname", ((EditText) findViewById(R.id.txtSurname)).getText())
                            .put("address", ((EditText) findViewById(R.id.txtAddress)).getText())
                            .put("phone", ((EditText) findViewById(R.id.txtPhone)).getText())
                            .put("email", ((EditText) findViewById(R.id.txtEmail)).getText())
                            .put("loginName", ((EditText) findViewById(R.id.txtUsername)).getText())
                            .put("password", ((EditText) findViewById(R.id.txtPassword)).getText());
                    user.put("user", detail);


                    HttpPost httpPost = new HttpPost(serverURL + "/user");
                    httpPost.setHeader("Content-Type", "application/json");
                    httpPost.setHeader("Accept", "application/json");
                    httpPost.setEntity(new ByteArrayEntity(user.toString().getBytes()));

                    HttpResponse response = client.execute(httpPost);
                    if (HttpStatus.SC_CREATED != response.getStatusLine().getStatusCode()) {
                        throw new RuntimeException("An error occured during registration");
                    } else {
                        Toast.makeText(getApplicationContext(), "User Registerd Successfully!", Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

}
